package models

type Img struct {
	BaseModel
	FileList string `json:"fileList" gorm:"not null;type:LONGTEXT"` //文件
	Name     string `json:"name" gorm:"not null;"`                  //车名
	TypesID  int    `json:"-" gorm:"not null;"`                     //类型
	Types    Types  `json:"types"`
}

func (u *Img) CreateTable() {
	err := DB.AutoMigrate(&Img{})
	if err != nil {
		panic("创建图片表失败:" + err.Error())
	}
}

func (u *Img) Add() error {
	return DB.Create(u).Error
}

func (c *Img) FindByPage(query map[string]int) ([]Img, int64, error) {
	var img []Img
	var total int64
	DB.Model(&Img{}).Where(c).Count(&total)
	result := DB.Limit(query["Limit"]).Offset(query["Offset"]).Preload("Types").Where(c).Find(&img)
	return img, total, result.Error

}

func (c *Img) Update(newDate interface{}) error {
	return DB.Model(&Img{}).Where(c).Updates(newDate).Error
}

func (c *Img) Delete() error {
	return DB.Delete(c).Error
}
